package com.lancoo.znbkxx.teacher.aibkTestCoach.questionToMark.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
import com.lancoo.znbkxx.teacher.R;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dadong on 2020/10/14
 * Describe:
 */
public class ZwPhotoAdapter extends RecyclerView.Adapter<ZwPhotoAdapter.ViewHolder> {

    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    private LayoutInflater mInflater;
    private List<LocalMedia> list = new ArrayList<>();
    private boolean isShowDelectIcon = true;
    private int selectMax = 9;
    private Context context;
    /**
     * 点击添加图片跳转
     */
    private onAddPicClickListener mOnAddPicClickListener;

    public interface onAddPicClickListener {
        void onAddPicClick();
    }

    public ZwPhotoAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public ZwPhotoAdapter(Context context, onAddPicClickListener mOnAddPicClickListener) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.mOnAddPicClickListener = mOnAddPicClickListener;
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    public void setList(List<LocalMedia> list) {
        this.list = list;
    }

    public boolean isShowDelectIcon() {
        return isShowDelectIcon;
    }

    public void setShowDelectIcon(boolean showDelectIcon) {
        isShowDelectIcon = showDelectIcon;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mIvAdd;
        ImageView mIvPhotoLoading;

        public ViewHolder(View view) {
            super(view);
            mIvAdd = view.findViewById(R.id.iv_add);
            mIvPhotoLoading = view.findViewById(R.id.iv_photoLoading);
        }
    }

    @Override
    public int getItemCount() {
        Log.e("==getItemCount==", "=============size==============" + list.size());
        if (isShowDelectIcon()) {
            if (list.size() < selectMax) {
                return list.size() + 1;
            } else {
                return list.size();
            }
        } else {
            return list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.adapter_item_photo_picker,
                viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }


    private boolean isShowAddItem(int position) {
        int size = list.size() == 0 ? 0 : list.size();
        return position == size;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int with = dm.widthPixels;
        int viewWith = with * 1 / 3;
        RelativeLayout.LayoutParams params_1 = (RelativeLayout.LayoutParams) viewHolder.mIvAdd.getLayoutParams();
        params_1.height = viewWith - 40;
        viewHolder.mIvAdd.setLayoutParams(params_1);

        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            viewHolder.mIvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddPicClickListener.onAddPicClick();
                }
            });
        } else {

            LocalMedia media = list.get(position);
            Log.e("getPath", "==============media.getPath()=====mmdd=================" + media.getPath());
            DrawableCrossFadeFactory drawableCrossFadeFactory = new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build();
            Glide.with(context).load(media.getPath())
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            viewHolder.mIvPhotoLoading.setVisibility(View.GONE);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            viewHolder.mIvPhotoLoading.setVisibility(View.GONE);
                            return false;
                        }
                    })
                    .apply(new RequestOptions().placeholder(R.mipmap.image_load_lose))
                    .transition(DrawableTransitionOptions.with(drawableCrossFadeFactory))
                    .into(viewHolder.mIvAdd);
            //itemView 的点击事件
            if (mItemClickListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("==mItemClickListener==", "=======查看原图=====》》》》》》》====");
                        int adapterPosition = viewHolder.getAdapterPosition();
                        mItemClickListener.onItemClick(adapterPosition, v);
                    }
                });
            }
        }
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}